import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;


public class calculation {

	protected Shell shell;
	private Text firstnumber;
	private Text secondnumber;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			calculation window = new calculation();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("Simple Calculation");
		
		firstnumber = new Text(shell, SWT.BORDER);
		firstnumber.setBounds(34, 35, 96, 21);
		
		secondnumber = new Text(shell, SWT.BORDER);
		secondnumber.setBounds(34, 117, 96, 21);
		
		Label lblFirstNumber = new Label(shell, SWT.NONE);
		lblFirstNumber.setBounds(34, 14, 82, 15);
		lblFirstNumber.setText("First number");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(34, 96, 100, 15);
		lblNewLabel.setText("Second number");
		
		final Label answerLabel = new Label(shell, SWT.NONE);
		answerLabel.setFont(SWTResourceManager.getFont("Script", 22, SWT.BOLD));
		answerLabel.setBounds(34, 214, 140, 32);
		answerLabel.setText("The answer is:");
		
		Button plusButton = new Button(shell, SWT.NONE);
		plusButton.setToolTipText("add");
		plusButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int n1, n2;
				try{
					n1=Integer.parseInt(firstnumber.getText());
				}catch(Exception exc){
					MessageDialog.openError(shell, "Error", "bat first number");
					return;
				}
				
				try{
					n2=Integer.parseInt(secondnumber.getText());
				}catch(Exception exc){
					MessageDialog.openError(shell, "Error", "bat second number");
					return;
				}
				int answer=n1+n2;
				answerLabel.setText("the answer is: "+answer);
			}
		});
		plusButton.setBounds(34, 164, 75, 25);
		plusButton.setText("+");
		
		Button minusButton = new Button(shell, SWT.NONE);
		minusButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int n1, n2;
				try{
					n1=Integer.parseInt(firstnumber.getText());
				}catch(Exception exc){
					MessageDialog.openError(shell, "Error", "bat first number");
					return;
				}
				
				try{
					n2=Integer.parseInt(secondnumber.getText());
				}catch(Exception exc){
					MessageDialog.openError(shell, "Error", "bat second number");
					return;
				}
				int answer=n1-n2;
				answerLabel.setText("the answer is: "+answer);
			}
		});
		minusButton.setToolTipText("subtract");
		minusButton.setBounds(118, 164, 75, 25);
		minusButton.setText("-");
		

	}
}
